export const pollFetch = async (callback,pollingInterval,delayTimeStatus = false,stop = false) => {
    if(stop) return
    const timeoutPromise = (delayTime) => {
        return new Promise(resolve => {
            window.timeId = setTimeout(resolve, delayTime);
        })
    }
    while (true) {
        const pollingTime = 1 * pollingInterval;
        const delayTime = pollingTime + (1000 * parseInt(Math.random() * 40, 10));     // 轮询时间间隔
        try {
            await callback();
            await timeoutPromise(delayTimeStatus ? delayTime : pollingTime);
        } catch (e) {
            await timeoutPromise(delayTimeStatus ? delayTime : pollingTime);
        }
    }
}


//时间格式化
export function formatDate(time, format = "YY-MM-DD hh:mm:ss") {
    var date = new Date(time);
    var year = date.getFullYear(),
      month = date.getMonth() + 1, //月份是从0开始的
      day = date.getDate(),
      hour = date.getHours(),
      min = date.getMinutes(),
      sec = date.getSeconds();
    var preArr = Array.apply(null, Array(10)).map(function (elem, index) {
      return "0" + index;
    });
    var newTime = format
      .replace(/YY/g, year)
      .replace(/MM/g, preArr[month] || month)
      .replace(/DD/g, preArr[day] || day)
      .replace(/hh/g, preArr[hour] || hour)
      .replace(/mm/g, preArr[min] || min)
      .replace(/ss/g, preArr[sec] || sec);

    return newTime;
}
